<?php
$url = '?md=' . MvcReg::$_moduleName . '&cl=' . MvcReg::$_controllerName . '&at=' . str_replace('add', '', MvcReg::$_actionName) . 'update';

$id = $name = $permission = $rank = $discription = '';
if (!empty($rsp_row)) {
    $row = $rsp_row;
    $id = $row["id"];
    $rank = $row["rank"];
    $permission = $row["permission"];
    $discription = $row["discription"];
    $name = ($row["name"]);
    $permission = preg_split('/[\s,]+/', $permission);
}
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include_once MvcReg::$_moduleName . '/views/layout/head.php'; ?>
</head>
<body>
<?php include_once MvcReg::$_moduleName . '/views/layout/woody-prompt.php'; ?>
<?php include_once MvcReg::$_moduleName . '/views/layout/header.php'; ?>
<div id="main">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-2">
                <?php include_once MvcReg::$_moduleName . '/views/layout/left_menu.php'; ?>
            </div>
            <div class="col-md-10">
                <div class="right-content">
                    <h3>
                        <ol class="breadcrumb"><li><a href="?md=manage&cl=store&at=company"><i class="fa fa-dashboard"></i><?php echo $menu["breadcrumb_current"]?></a><li class="active"><?php echo ((!empty($id) && ($id > 0)) > 0 ? '%{EDIT}%' : '%{ADD}%')?></li></li></ol>
                    </h3>
                    <br>
                    <form class="form-horizontal" action="<?php echo $url?>"
                          method="post" autocomplete="off">
                        <div class="form-group">
                            <label for="cname" class="col-md-2 control-label">
                                <i>*</i>排序：
                            </label>
                            <div class="col-md-6">
                                <input type="text" name ="rank" class="form-control " placeholder="数字越小越靠前" value="<?php echo $rank; ?>" required/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="title" class="col-md-2 control-label">
                                <i>*</i>权限名称：
                            </label>
                            <div class="col-md-6">
                                <input type="text" name ="name" class="form-control " placeholder="" value="<?php echo $name ?>" required/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="addtime" class="col-md-2 control-label"><input type="checkbox" id="allgroup" name="allgroup"/>权限项目</label>
                            <div class="col-md-6">
                                <?php
                                $list = Data::getAllAuthList('1');
                                $data = "";
                                $i = 0;
                                foreach ($list as $k => $v) {
                                    $check = !empty($permission) && (in_array($k, $permission)) ? ' checked="checked" ' : '';
                                    $data.='<div class="checkbox"><li><input type="checkbox" value="' . $k . '"  name="group[]" class="checkMain gr' . $i . '0" ' . $check . ' /><span>' . $v['name'] . '</span>';
                                    if (isset($v['sub'])) {
                                        $sub = "<ul style='padding-left: 40px;'>";
                                        foreach ($v['sub'] as $f => $value) {
                                            $check = !empty($permission) && (in_array($f, $permission)) ? ' checked="checked"' : '';
                                            $sub.='<li><input type="checkbox" class="checkSub gr' . $i . '"  value="' . $f . '" name="group[]" ' . $check . ' /><span>' . $value . '</span></li>';
                                        }
                                        $i++;
                                        $sub.="<ul>";
                                        $data.=$sub;
                                    }
                                    $data.="</li></div>";
                                }
                                echo $data;
                                ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="cname" class="col-md-2 control-label">
                                权限描述：
                            </label>
                            <div class="col-md-6">
                                <input type="text" name ="discription" class="form-control" placeholder="" value="<?php echo $discription; ?>"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="stacode" class="col-md-2 control-label">
                            </label>
                            <div class="col-md-4">
                                <button type="submit" class="btn btn-success">保存</button>
                            </div>
                            <span class="col-md-6"></span>
                        </div>
                        <input type="hidden" id="page" name="page" value="<?php echo $page?>"/>
                        <input type="hidden" id="id" name="id" value="<?php echo $id?>"/>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include_once MvcReg::$_moduleName . '/views/layout/footer.php'; ?>

<script>
    $(function () {

        $('.checkSub').click(function () {
            //alert($(this).attr('class').substr(9));
            var c = $(this).attr('class').substr(9);
            var len = $('.col-sm-4').find('.' + c).length;
            var len_check = $('.col-sm-4').find('.' + c + ":checked").length;
            if (len == len_check) {
                //alert("等于："+len);
                $('.col-sm-4').find('.' + c + "0").prop("checked", "checked");
            } else {
                //alert("NO"+len);
                $('.col-sm-4').find('.' + c + "0").removeAttr('checked');
            }
        });

        $('.checkMain').each(function () {
            $(this).click(function () {
                if ($(this).is(':checked')) {

                    $(this).parent('li').find('.checkSub').prop("checked", "checked");

                } else {
                    $(this).parent('li').find('.checkSub').removeAttr('checked')
                }
            });
        });

        $('#allgroup').bind("click", function(){
            //alert(1);
            if ($(this).is(':checked')) {
                $('.checkMain').prop("checked", "checked");
                $('.checkSub').prop("checked", "checked");
            } else {
                $('.checkMain').prop("checked", false);
                $('.checkSub').prop("checked", false);
            }
        });
    });
</script>
</html>